package thread;

public class Demo15 {
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Object locker2 = new Object();
        // String s = new String();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
            System.out.println("t1 结束");
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
            System.out.println("t2 结束");
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        // 一个线程自增 5w 次, 两个线程, 总共自增 10w 次. 预期结果, count = 100000
        System.out.println(count);
    }
}
